package web.get;

import com.fasterxml.jackson.databind.ObjectMapper;
import domain.User;
import service.WordService;
import service.impl.WordServiceImpl;
import util.ServletUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/getLoginUserInfo")
public class GetLoginUserInfoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = ServletUtil.checkLogin(request);
        ObjectMapper om = new ObjectMapper();
        Map<Object, Object> msg;
        int uid=0;
        WordService ws = new WordServiceImpl();
        if(user!=null){
            //去除username中的 password
            user.setPassword("");
            uid= user.getUid();
        }
        String msgStringTemp = om.writeValueAsString(user);
        msg = om.readValue(msgStringTemp, Map.class);
        msg.put("remove",ws.getRemoveWordNum(uid));
        msg.put("know",ws.getKnowWordNum(uid));
        msg.put("familiar",ws.getFamiliarWordNum(uid));
        msg.put("execute",ws.getExecuteWordNum(uid));
        //返回User信息
        om.writeValue(response.getOutputStream(),msg);
    }
}
